package com.lieve.tomcat;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunlijiang
 * @date 2020/4/2
 */
public class MyThreadLocal<T> {
    Map<Thread, T> map = new HashMap<>();

    public void set(T value) {
        Thread t = Thread.currentThread();
        map.put(t, value);
    }

    public T get() {
        Thread t = Thread.currentThread();
        T value = map.get(t);
        if (value == null && !map.containsKey(t)) {
            value = initValue();
            map.put(t, value);
        }
        return value;
    }


    protected T initValue() {
        return null;
    }

    public void remove() {
        map.remove(Thread.currentThread());
    }


}
